/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.samlss.lighter_demo.fragment;

import me.samlss.lighter.Lighter;
import me.samlss.lighter.interfaces.OnLighterViewClickListener;
import me.samlss.lighter.parameter.Direction;
import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.parameter.MarginOffset;
import me.samlss.lighter.shape.OvalShape;
import me.samlss.lighter_demo.LighterHelper;
import me.samlss.lighter_demo.ResourceTable;
import me.samlss.lighter_demo.tools.Toast;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class LighterFragment extends Fraction implements Component.ClickedListener {
    private static LighterFragment fragment;
    private Lighter mLighter;
    private StackLayout viewpager_root;
    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private Button mButton5;

    private List<Component> mTipViewList = new ArrayList<>();

    private LighterFragment() {
    }

    /**
     * Fraction单例
     * @return Fraction
     */
    public static Fraction newInstance() {
        if(fragment == null) {
            fragment = new LighterFragment();
        }
        return fragment;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_viewpager_1, container, false);
        initView(component);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void onActive() {
        super.onActive();

    }

    private void initView(Component component) {
        viewpager_root = (StackLayout) component.findComponentById(ResourceTable.Id_viewpager_root);
        mButton1 = (Button) component.findComponentById(ResourceTable.Id_vp_btn_1);
        mButton1.setClickedListener(this);
        mButton2 = (Button) component.findComponentById(ResourceTable.Id_vp_btn_2);
        mButton2.setClickedListener(this);
        mButton3 = (Button) component.findComponentById(ResourceTable.Id_vp_btn_3);
        mButton3.setClickedListener(this);
        mButton4 = (Button) component.findComponentById(ResourceTable.Id_vp_btn_4);
        mButton4.setClickedListener(this);
        mButton5 = (Button) component.findComponentById(ResourceTable.Id_vp_btn_5);
        mButton5.setClickedListener(this);

        Component tipView1 = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_layout_tip_1, viewpager_root, false);
        Component tipView2 = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_layout_tip_7, viewpager_root, false);
        Component tipView3 = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_layout_tip_2, viewpager_root, false);
        Component tipView4 = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_layout_tip_3, viewpager_root, false);
        Component tipView5 = LayoutScatter.getInstance(getFractionAbility()).parse(ResourceTable.Layout_layout_tip_4, viewpager_root, false);

        mTipViewList.add(tipView1);
        mTipViewList.add(tipView2);
        mTipViewList.add(tipView3);
        mTipViewList.add(tipView4);
        mTipViewList.add(tipView5);
        showGuide();
    }

    private void showGuide() {
        mLighter = Lighter.with(viewpager_root)
                .setBackgroundColor(0x000000, 0.5f)
                .setIntercept(true)
                .setOnClickListener(new OnLighterViewClickListener() {
                    @Override
                    public void onClick(Component component) {
//                        Toast.show(getContext(),"No show...", Toast.LENGTH_SHORT);
                    }
                })
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_1)
                        .setLighterShape(new OvalShape())
                        .setTipView(mTipViewList.get(0))
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .setTipViewRelativeDirection(Direction.RIGHT)
                        .setTipViewRelativeOffset(new MarginOffset(30, 0, 80, 0))
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_2)
                        .setLighterShape(new OvalShape())
                        .setTipView(mTipViewList.get(1))
                        .setTipViewRelativeDirection(Direction.LEFT)
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .setTipViewRelativeOffset(new MarginOffset(50, 0, 100, 0))
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_3)
                        .setLighterShape(new OvalShape())
                        .setTipView(mTipViewList.get(2))
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .setTipViewRelativeOffset(new MarginOffset(-400, 0, 0, 30))
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_4)
                        .setLighterShape(new OvalShape())
                        .setTipView(mTipViewList.get(3))
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewRelativeOffset(new MarginOffset(80, 0, 0, 20))
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_5)
                        .setLighterShape(new OvalShape())
                        .setTipView(mTipViewList.get(4))
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewRelativeOffset(new MarginOffset(0, 100, 0, 20))
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .build());
        mLighter.show();
    }

    private void showNext(){
        if (mLighter != null){
            mLighter.next();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mTipViewList.clear();
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_vp_btn_1:
                showNext();
                Toast.show(getFractionAbility(),"You click button 1", Toast.LENGTH_SHORT);
                break;
            case ResourceTable.Id_vp_btn_2:
                showNext();
                Toast.show(getFractionAbility(),"You click button 2", Toast.LENGTH_SHORT);
                break;
            case ResourceTable.Id_vp_btn_3:
                showNext();
                Toast.show(getFractionAbility(),"You click button 3", Toast.LENGTH_SHORT);
                break;
            case ResourceTable.Id_vp_btn_4:
                showNext();
                Toast.show(getFractionAbility(),"You click button 4", Toast.LENGTH_SHORT);
                break;
            case ResourceTable.Id_vp_btn_5:
                showNext();
                Toast.show(getFractionAbility(),"You click button 5", Toast.LENGTH_SHORT);
                break;
        }
    }
}
